package com.beyond.trace.controller.agricultural;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beyond.trace.domain.agricultural.AgriculturalIdentify;
import com.beyond.trace.domain.agricultural.AgriculturalSuggest;
import com.beyond.trace.service.agricultural.IAgriculturalSuggestService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 农技防控建议
 * @Author: yaojianbo
 * @Date: 2023-05-22 11:37
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/agricultural")
@Api(value = "农技防控建议", tags = "农技防控建议")
public class AgriculturalSuggestController extends BaseController {

    @Autowired
    private IAgriculturalSuggestService agriculturalSuggestService;

    @GetMapping("/getSuggestList")
    @ApiOperation("查询农技防控建议列表")
    public TableDataInfo list(AgriculturalSuggest agriculturalSuggest)
    {
        startPage();
        List<AgriculturalSuggest> list = agriculturalSuggestService.getSuggestList(agriculturalSuggest);
        return getDataTable(list);
    }


    @GetMapping(value = "/getSuggestById")
    @ApiOperation("通过id查询农技防控建议")
    public AjaxResult getInfo(Long id)
    {
        if(id == null) {
            return AjaxResult.error("id不能为空");
        }
        return AjaxResult.success(agriculturalSuggestService.getById(id));
    }

    @PostMapping("/addSuggest")
    @ApiOperation("新增农技防控建议")
    public AjaxResult add(@RequestBody AgriculturalSuggest agriculturalSuggest)
    {
        agriculturalSuggest.setId("");
        agriculturalSuggest.setCreateTime(DateUtils.getNowDate());
        agriculturalSuggest.setUpdateTime(DateUtils.getNowDate());
        agriculturalSuggest.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        agriculturalSuggest.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        agriculturalSuggest.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        agriculturalSuggest.setStatus(0);
        return toAjax(agriculturalSuggestService.save(agriculturalSuggest));
    }

    @PostMapping("/editSuggest")
    @ApiOperation("修改农技防控建议")
    public AjaxResult edit(@RequestBody AgriculturalSuggest agriculturalSuggest)
    {
        if(StringUtils.isEmpty(agriculturalSuggest.getId())) {
            return AjaxResult.error("id不能为空");
        }
        AgriculturalSuggest byId = agriculturalSuggestService.getById(agriculturalSuggest.getId());
        if(byId == null) {
            return AjaxResult.error("信息未找到");
        }
        if(byId.getStatus() == 1) {
            return AjaxResult.error("该信息已发布，情先下架以后再修改");
        }
        agriculturalSuggest.setUpdateTime(DateUtils.getNowDate());
        agriculturalSuggest.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(agriculturalSuggestService.updateById(agriculturalSuggest));
    }

    @GetMapping("/deleteSuggestByIds")
    @ApiOperation("删除农技防控建议")
    public AjaxResult remove(String ids)
    {
        if(StringUtils.isEmpty(ids)) {
            return AjaxResult.error("id不能为空");
        }
        List<String> list = Arrays.asList(ids.split(","));
        List<AgriculturalSuggest> agriculturalSuggestList = agriculturalSuggestService.listByIds(list);
        if(agriculturalSuggestList.size() != list.size()) {
            String temp = "";
            for (AgriculturalSuggest s : agriculturalSuggestList) {
                if(!list.contains(s.getId())) {
                    temp = temp + s.getId();
                }
            }
            return AjaxResult.error("id信息未找到： "+temp);
        }
        for (AgriculturalSuggest s : agriculturalSuggestList) {
            if(s.getStatus() != 0) {
                return AjaxResult.error("信息未下架，请先下架以后再进行删除： "+s.getId());
            }
        }

        return toAjax(agriculturalSuggestService.removeByIds(list));
    }

    @GetMapping("/onlineSuggest")
    @ApiOperation("发布农技防控建议")
    public AjaxResult onlineSuggest(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        AgriculturalSuggest byId = agriculturalSuggestService.getById(id);
        if(byId == null) {
            return AjaxResult.error("信息未找到");
        }
        if(byId.getStatus() != 0) {
            return AjaxResult.error("该信息已上架");
        }
        AgriculturalSuggest agriculturalSuggest = new AgriculturalSuggest();
        agriculturalSuggest.setId(id);
        agriculturalSuggest.setStatus(1);
        agriculturalSuggest.setUpdateTime(DateUtils.getNowDate());
        agriculturalSuggest.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(agriculturalSuggestService.updateById(agriculturalSuggest));
    }

    @GetMapping("/offlineSuggest")
    @ApiOperation("下架农技防控建议")
    public AjaxResult offlineSuggest(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        AgriculturalSuggest byId = agriculturalSuggestService.getById(id);
        if(byId == null) {
            return AjaxResult.error("信息未找到");
        }
        if(byId.getStatus() != 1) {
            return AjaxResult.error("该信息已下架");
        }
        AgriculturalSuggest agriculturalSuggest = new AgriculturalSuggest();
        agriculturalSuggest.setId(id);
        agriculturalSuggest.setStatus(0);
        agriculturalSuggest.setUpdateTime(DateUtils.getNowDate());
        agriculturalSuggest.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(agriculturalSuggestService.updateById(agriculturalSuggest));
    }

    @GetMapping("/getSuggestSelectData")
    @ApiOperation("获取下拉选择")
    public AjaxResult getSelectData(String departId,String online)
    {
        LambdaQueryWrapper<AgriculturalSuggest> queryWrapper = new LambdaQueryWrapper<>();
        //传了token优先使用，如果未传token传了departId 用depaartid
        String depart = departId;
        try {
            if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                depart = String.valueOf(SecurityUtils.getUserDepartId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        if(StringUtils.isNotEmpty(depart)) {
            queryWrapper.eq(AgriculturalSuggest::getDepartId,depart);
        }
        if(StringUtils.isNotEmpty(online)) {
            queryWrapper.eq(AgriculturalSuggest::getStatus,1);
        }
        queryWrapper.groupBy(AgriculturalSuggest::getCycle);
        List<String> collect = agriculturalSuggestService.list(queryWrapper).stream().map(s -> s.getCycle()).collect(Collectors.toList());
        return AjaxResult.success(collect);
    }

    @GetMapping("/getScreenSuggestHH")
    @ApiOperation("大屏获取红花基地")
    public AjaxResult getScreenSuggestHH(String name)
    {
        LambdaQueryWrapper<AgriculturalSuggest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgriculturalSuggest::getDepartId,101)
                .eq(AgriculturalSuggest::getStatus,1);
        if(StringUtils.isNotEmpty(name)) {
            queryWrapper.eq(AgriculturalSuggest::getCycle,name);
        }
        queryWrapper.orderByDesc(AgriculturalSuggest::getUpdateTime);
        return AjaxResult.success(agriculturalSuggestService.list(queryWrapper));
    }

    @GetMapping("/getScreenSuggestCYG")
    @ApiOperation("大屏获取茶叶谷基地")
    public AjaxResult getScreenSuggestCYG(String name)
    {
        LambdaQueryWrapper<AgriculturalSuggest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AgriculturalSuggest::getDepartId,105)
                .eq(AgriculturalSuggest::getStatus,1);
        if(StringUtils.isNotEmpty(name)) {
            queryWrapper.eq(AgriculturalSuggest::getCycle,name);
        }
        queryWrapper.orderByDesc(AgriculturalSuggest::getUpdateTime);
        return AjaxResult.success(agriculturalSuggestService.list(queryWrapper));
    }


}
